<?php


namespace app\Common\tools;


//use App\Common\model\Member;
use app\Exceptions\ApiException;
use think\facade\Db;

class ApiMemberInfo
{
    private static $instance;
    public $member;

    private function __construct($id)
    {
        $this->member = Db::connect('zhu')->table('cms_member')->where('userid',$id)->find();
    }

    /**
     * @param string $id
     * @return ApiMemberInfo
     * @throws ApiException
     * @author 刘宏盟
     */
    public static function Instance($id = '') {
        if(self::$instance) {
            return self::$instance;
        }
        if(!$id) {
            echo json_encode(['status'=>0,'msg'=>'请登录之后在操作']);
            exit();
        }
        self::$instance = new ApiMemberInfo($id);
        return self::$instance;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Model|null
     * @author 刘宏盟
     */
    public function info()
    {
        $info = Db::table('ea_member')->where('member_id',$this->member['userid'])->find();

        if (!$info){
            $getid = Db::table('ea_member')->insertGetId([
                'member_id'=>$this->member['userid'],
                'phone'=>$this->member['mobile']
            ]);
            Db::table('ea_member_detail')->insert(['member_id'=>$getid]);
        }

        $ea_member = Db::table('ea_member')->alias('e')
            ->join('member_detail m','e.id = m.member_id')
            ->where('e.member_id',$this->member['userid'])
            ->field('e.id,e.name,e.education_level,e.phone,e.sex,
            e.participation_time,e.identity,e.wx_id,e.birthday,e.default_identity,m.advantage,m.working_status,e.email
            ')
            ->find();

        $ea_member['avatar'] = Db::connect('zhu')->table('cms_member_detail')->where('userid',$this->member['userid'])->value('avatar');

        Db::table('ea_member')->where('member_id',$this->member['userid'])->update(['avatar'=>$ea_member['avatar']]);

        return $ea_member;
    }

}
